<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>支付</title>
    <link rel="stylesheet" href="/shop/css/reset.css">
    <link rel="stylesheet" href="/shop/css/main.css">
    <script type="text/javascript">
        SITEURL = '{:C("SITE_URL")}';
    </script>
</head>
<body>
<ul class="pay-list">
    <li class="bgf mb">
        <div class="pay-wp">
            <h3 class="tit">联系人</h3>
            <div class="info">
                <input id="orderName" type="text" value="{$order['member_name']}" placeholder="您的收餐姓名" /><em></em>
            </div>
        </div>
        <div class="pay-wp">
            <h3 class="tit">联系方式</h3>
            <div class="info">
                <input id="orderPhone" type="text" value="{$order['member_name']}" placeholder="您的收餐电话" /><em></em>
            </div>
        </div>

        <div class="pay-wp">
            <h3 class="tit">送餐楼宇</h3>
            <div class="info">
                <!--<input id="orderReginName" type="text" value="" placeholder="您的收餐楼宇" /><em></em>-->
                <span id="orderReginName" class="a0"></span>
            </div>
        </div>
        <div class="pay-wp">
            <h3 class="tit">详细地址</h3>
            <div class="info">
                <input id="orderAddress" type="text" value="" placeholder="您的收餐地址" /><em></em>
            </div>
        </div>

    </li>
    <li class="bor mb">
        <div class="pay-wp">
            <h3 class="tit">送餐时间</h3>
            <div class="info">
                <span id="orderTime" class="a0">12:00-12:20</span>
            </div>
        </div>
    </li>
    <li class="bgf">
        <div class="pay-quan">
            <h3 class="tit">使用礼券</h3>
            <span id="couponValue">不使用礼券</span>
        </div>
    </li>
    <li class="bgf" id="orderGoods">
        <div class="pay-cont-wp">
            <h3 class="tit">周一</h3>
            <dl class="list">
                <dd>
                    <p>香菇滑鸡煲老鸭炖盅套餐</p>
                    <span>x100</span>
                </dd>
                <dd>
                    <p>香菇滑鸡煲老鸭炖盅套餐</p>
                    <span>x100</span>
                </dd>
            </dl>
            <span class="pase">88元</span>
        </div>
        <div class="pay-cont-wp">
            <h3 class="tit">周二</h3>
            <dl class="list">
                <dd>
                    <p>香菇滑鸡煲老鸭炖盅套餐</p>
                    <span>x100</span>
                </dd>
            </dl>
            <span class="pase">88元</span>
        </div>
    </li>
    <li class="bgf">
        <div class="pay-wp">
            <h3 class="tit">餐品合计</h3>
            <div class="info">
                <span id="orderTotal">156元</span>
            </div>
        </div>
        <div class="pay-wp">
            <h3 class="tit">使用礼券</h3>
            <div class="info">
                <span id="orderDiscount">-12元</span>
            </div>
        </div>
        <div class="pay-wp">
            <h3 class="tit z">总计</h3>
            <div class="info">
                <span class="z" id="orderAmount">200 元</span>
            </div>
        </div>
    </li>


    <li class="yu bgf">
        <div class="pay-cont-wp">
            <span class="zhi-z"></span>
            <p class="xin">微信支付</p>
            <em class="zed on"></em>
        </div>
    </li>
</ul>

<div class="zhi-btn-wp">
    <div class="zhi-btn">立即下单</div>
</div>


</body>
<script type="text/javascript" src="/shop/js/jquery.min.js"></script>
<script type="text/javascript">
    /**
     * Created by EiT on 2016/12/19.
     */
    /* 支付验证 */
    function IsChinese(name) {
        var str = $.trim($(name).val());
        if(str == '') {
            salert("对不起，姓名不能为空!");
            return false;
        }else{
            return true;
        }
    }
    function IsPhone(name) {
        var str = $(name).val();
        var reg =/^1[3,4,5,7,8]\d{9}$/; //是不是手机号码
        if(!reg.test(str)) {
            salert("对不起，您输入的手机号码不正确!");
            return false;
        }else{
            return true;
        }
    }
    function IsAddress(name) {
        var str = $.trim($(name).val());
        if(str=='') {
            salert("对不起，请您输入地址!");
            return false;
        }else{
            return true;
        }
    }
    //姓名验证
    $("#orderName").on("blur",function(){
        IsChinese("#orderName");
    })
    //手机号验证
    $("#orderPhone").on("blur",function(){
        IsPhone("#orderPhone");
    })
    //地址标签的隐藏
    $("#orderAddress").on("blur",function(){
        IsAddress("#orderAddress");
    });

    var cart = JSON.parse(localStorage.getItem('cart'));
    $.post(SITEURL + 'wechat/shop/submitCart',{cart:cart},function(data){
        if(data.msg=='ok'){
            subOrder(data.data);
        }else{
            salert(data.msg);
        }
    });

    //提交订单操作
    function subOrder(obj){

        console.log(obj);

        if(obj.address){
            $('#orderAddress').val(obj.address);
        }

        if(obj.member_name){
            $('#orderName').val(obj.member_name);
        }
        if(obj.member_phone){
            $('#orderPhone').val(obj.member_phone);
        }

        //设置送餐时间
        $('#orderTime').text(obj.deliver_time);
        $('#orderReginName').text(obj.region_name);
        var couponValue = obj.coupon_value==0?'不使用礼券':'-'+obj.coupon_value+'元';
        $('#couponValue').text(couponValue);


        //设置用户点餐列表
        var orderList = '';
        var menu = obj.menu;
        var l = menu.length;
        for(var i=0;i<l;i++){
            orderList += ('<div class="pay-cont-wp" menuId="'+menu[i].menu_id+'"><h3 class="tit">'+menu[i].menu_week+'<br/>('+menu[i].menu_date+')</h3><dl class="list">');
            var goods = menu[i].goods;
            var gl = goods.length;
            for(var c=0;c<gl;c++){
                orderList += ('<dd><p>'+goods[c].goods_name+'</p><span>x'+goods[c].goods_num+'</span></dd>');
            }
            orderList += ('</dl><span class="pase">'+menu[i].goods_price+'元</span></div>');
        }
        $('#orderGoods').html(orderList);

        $('#orderTotal').text(obj.goods_amount+'元');
        $('#orderDiscount').text('-'+obj.discount_amount+'元');
        $('#orderAmount').text(obj.order_amount+'元');

        //点击支付
        $('.zhi-btn').click(function(){
            if(IsChinese("#orderName")&&IsPhone("#orderPhone")&&IsAddress("#orderAddress")){
                $.ajax({
                    url: SITEURL + 'wechat/shop/submitOrder/',
                    dataType: "json",
                    data: {
                        "region_id": obj.region_id,
                        "member_name": $("#orderName").val(),
                        "member_phone": $("#orderPhone").val(),
                        "region_name": obj.region_name,
                        "address": $("#orderAddress").val()
                    },
                    type: "POST",
                    beforeSend: function() { salert("订单支付中"); },
                    success: function(data) {
                        if(data.code == 0){
                            callpay(data.data);
                        } else {
                            salert(data.msg);
                        }

                    },
                    error: function() {
                        salert("订单支付失败，请重新支付");
                    }
                });
            }
            //下单成功,清除localStorage 也就是你说的cookie
             localStorage.removeItem("cart");
        });

    }

    function jsApiCall(data){
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            data,
            function(res){
                //WeixinJSBridge.log(res.err_msg);
                //alert(res.err_code + res.err_desc + res.err_msg);
                if(res.err_msg == "get_brand_wcpay_request:ok"){
                    window.location = SITEURL + 'wechat/shop/finish';
                } else {
                    salert("支付取消，请重新支付");
                }
            }
        );
    }

    function callpay(data){
        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        }else{
            jsApiCall(data);
        }
    }
    </script>
</html>